package com.ruoyi.womp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.womp.domain.WompSparePartsInout;
import com.ruoyi.womp.service.ISqlQueryService;
import com.ruoyi.womp.service.IWompSparePartsInoutService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 备件出入库Controller
 * 
 * @author mzz
 * @date 2023-05-11
 */
@Controller
@RequestMapping("/womp/sparePartsInOut")
public class WompSparePartsInoutController extends BaseController {
	private String prefix = "womp/sparePartsInOut";

	@Autowired
	private IWompSparePartsInoutService wompSparePartsInoutService;

	@Autowired
	ISqlQueryService sqlQueryService;

	@RequiresPermissions("womp:sparePartsInOut:view")
	@GetMapping()
	public String sparePartsInOut() {
		return prefix + "/sparePartsInOut";
	}

	/**
	 * 查询备件出入库列表
	 */
	@RequiresPermissions("womp:sparePartsInOut:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WompSparePartsInout wompSparePartsInout) {
		startPage();

		String sql = " select wspi.id, wspi.spare_part_id sparePartId, wspi.inout_type inoutType, wspi.num, wspi.remark, wspi.create_time createTime, wspi.create_by createBy, "
				+ " wspi.update_time updateTime, wspi.update_by updateBy,wsp.model,wsp.spec from womp_spare_parts_inout wspi "
				+ " left join womp_spare_parts wsp on wspi.spare_part_id=wsp.id where 1=1 ";

		if (null != wompSparePartsInout.getSparePartId()) {
			sql += " and spare_part_id=" + wompSparePartsInout.getSparePartId();
		}

		if (null != wompSparePartsInout.getInoutType()) {
			sql += " and inout_type=" + wompSparePartsInout.getInoutType();
		}

		sql+=" order by wspi.create_time desc ";
		List<Map<String, Object>> list = sqlQueryService.queryMapList(sql);

		return getDataTable(list);
	}

	/**
	 * 导出备件出入库列表
	 */
	@RequiresPermissions("womp:sparePartsInOut:export")
	@Log(title = "备件出入库", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(WompSparePartsInout wompSparePartsInout) {
		List<WompSparePartsInout> list = wompSparePartsInoutService.selectWompSparePartsInoutList(wompSparePartsInout);
		ExcelUtil<WompSparePartsInout> util = new ExcelUtil<WompSparePartsInout>(WompSparePartsInout.class);
		return util.exportExcel(list, "备件出入库数据");
	}

	/**
	 * 新增备件出入库
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存备件出入库
	 */
	@RequiresPermissions("womp:sparePartsInOut:add")
	@Log(title = "备件出入库", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WompSparePartsInout wompSparePartsInout) {
		return toAjax(wompSparePartsInoutService.insertWompSparePartsInout(wompSparePartsInout));
	}

	/**
	 * 修改备件出入库
	 */
	@RequiresPermissions("womp:sparePartsInOut:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		WompSparePartsInout wompSparePartsInout = wompSparePartsInoutService.selectWompSparePartsInoutById(id);
		mmap.put("wompSparePartsInout", wompSparePartsInout);
		return prefix + "/edit";
	}

	/**
	 * 修改保存备件出入库
	 */
	@RequiresPermissions("womp:sparePartsInOut:edit")
	@Log(title = "备件出入库", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WompSparePartsInout wompSparePartsInout) {
		return toAjax(wompSparePartsInoutService.updateWompSparePartsInout(wompSparePartsInout));
	}

	/**
	 * 删除备件出入库
	 */
	@RequiresPermissions("womp:sparePartsInOut:remove")
	@Log(title = "备件出入库", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(wompSparePartsInoutService.deleteWompSparePartsInoutByIds(ids));
	}

	/**
	 * 备件入库
	 */
	@GetMapping("/in/{id}")
	public String in(@PathVariable("id") Integer id, ModelMap mmap) {

		String sql = " select wsp.id,wsp.name,wsp.model,wsp.spec,wsp.productue,wsp.remark,wsp.file_source, "
				+ " ifnull((select sum(num) from womp_spare_parts_inout wspi where wspi.spare_part_id=" + id
				+ "),0) num from womp_spare_parts wsp where id =" + id;
		Map<String, Object> map = sqlQueryService.queryOne(sql);
		mmap.put("wompSpareParts", map);
		return prefix + "/in";
	}

	/**
	 * 保存备件出入库
	 */
	@Log(title = "备件出入库", businessType = BusinessType.INSERT)
	@PostMapping("/in")
	@ResponseBody
	public AjaxResult in(WompSparePartsInout wompSparePartsInout) {
		return toAjax(wompSparePartsInoutService.insertWompSparePartsInout(wompSparePartsInout));
	}

	/**
	 * 备件出库
	 */
	@GetMapping("/out/{id}")
	public String out(@PathVariable("id") Integer id, ModelMap mmap) {

		String sql = " select wsp.id,wsp.name,wsp.model,wsp.spec,wsp.productue,wsp.remark,wsp.file_source, "
				+ " ifnull((select sum(num) from womp_spare_parts_inout wspi where wspi.spare_part_id=" + id
				+ "),0) num from womp_spare_parts wsp where id =" + id;
		Map<String, Object> map = sqlQueryService.queryOne(sql);
		mmap.put("wompSpareParts", map);
		return prefix + "/out";
	}

	/**
	 * 保存备件出入库
	 */
	@Log(title = "备件出入库", businessType = BusinessType.INSERT)
	@PostMapping("/out")
	@ResponseBody
	public AjaxResult out(WompSparePartsInout wompSparePartsInout) {
		wompSparePartsInout.setNum(-wompSparePartsInout.getNum());
		return toAjax(wompSparePartsInoutService.insertWompSparePartsInout(wompSparePartsInout));
	}
}
